package com.imooc.timer.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Service;

/**
 * <p>
 * redis订阅模式
 * 延时消息
 * </p>
 *
 * @author gzj
 * @since 2023/11/1
 */
@Service
@Slf4j
public class ExpirationListener extends KeyExpirationEventMessageListener {
    public ExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        //获取过期的key
        String expireKey = message.toString();
        log.debug("失效+key is:"+ expireKey);
        System.out.println(expireKey);
        //这里还可以根据key的自定义前缀来判断执行哪个条件
        //......
    }
}
